package org.oschina.api.manage.plugin.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.manage.plugin.service.PluginService;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.plugin.bean.PluginBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value=Config.API_PATH+"/manage/plugin")
public class PluginController extends BaseController {

	@Autowired
	private PluginService pluginService;
	
	/**
	 * 返回所有插件
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<PluginBean> get()
	{
		return pluginService.get();
	}
	
	/**
	 * 设置激活
	 * @param id
	 * @param enabled
	 * @return
	 */
	@Validate
	@RequestMapping(value="/setEnabled/{id}",method=RequestMethod.POST)
	public Result setEnabled
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField()) Boolean enabled
	)
	{
		return pluginService.setEnabled(id, enabled);
	}
	
	/**
	 * 添加插件
	 * @param clazz
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Result add(MultipartFile file)
	{
		return pluginService.add(toFile(file));
	}
	
	/**
	 * 移除插件
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/remove/{id}",method=RequestMethod.POST)
	public Result remove
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return pluginService.remove(id);
	}
}